﻿import com.dinmax.flash.gsa.communication.*
import com.dinmax.flash.gsa.exception.*
import com.dinmax.flash.gsa.exception.communication.*
import com.dinmax.flash.gsa.sgs.game.*
import com.dinmax.flash.gsa.sgs.state.*
import com.dinmax.flash.gsa.sgs.*
import com.dinmax.flash.gsa.sgs.game.*
import com.dinmax.flash.gsa.sgs.bridge.*
import com.dinmax.flash.gsa.games.gameSlot.*

class SGS
implements CommunicationEventListener, SlotInterface {

private var serial:String;
private var bridge:SGSBridge;
private var games:SGSGames;
private var state:State;
private var gameSlot:GameSlotInterface;


private static var instance:SGS;


public static function RegisterGame(game:GameSlotInterface):SlotInterface {
	//trace("SGS.as | RegisterGame "+game);
	instance.gameLogIn(game);
	return instance;
}; //fin función


public static function getActive():SGS {
	return instance;
}


public static function newInstance(bridge:SGSBridge, serial:String):Void {
	//trace("SGS.as | newInstance("+bridge+" ,"+serial);
	instance=new SGS(bridge, serial);	
}

function SGS(bridge:SGSBridge, serial:String) {
		//Guardo la referencia al bridge y el serial de la maquina
		this.bridge=bridge;
		this.serial=serial;

		//Creo la conexion con el server.
		Communication.newInstance(this);	
		
		//Arranco deshabilitada
		state=new Disabled();
		state.init();
}


public function getSerial():String {
		return serial;
	}


public function setSerial(serial:String):Void {
		this.serial = serial;
	}


public function getBridge():SGSBridge {
	return bridge;
}; //fin función



public function getState():State {
		return state;
}


public function setState(state:State):Void {
		this.state = state;
}


public function registerMachine(serial:String):Void {
	try {
			Communication.getActive().registerMachine(serial);
			bridge.SGS_2_SWF_inCommunication();
			
	} catch (e) {
			trace("SGS.as | 117 | Error en registerMachine "+e);
	}
}; //fin función


public function playerLogIn(serial:String):Void {
	try {
		Communication.getActive().playerLogIn(serial);
		getBridge().SGS_2_SWF_inCommunication();
	} catch (e) {
		trace("SGS.as | 125 | Error en playerLogIn "+e);
	};
}; //fin función


public function playerLogOut():Void {
	try {
		Communication.getActive().playerLogOut();
	} catch (e) {
		trace("SGS.as | 108 | Error en playerLogOut "+e);
	};
}; //fin función



public function play(bet:Bet):Void{
	//trace("SGS.as | play bet= " + bet.getTokens());
	Communication.getActive().gamePlay(bet);
};	

public function setActiveGame(game:Game):Void {
	if(!Active(state).setActiveGame(game)){
		throw new Error("estado incorrecto");
	} else {
		bridge.SGS_2_SWF_activateGame(Active(state).getActiveGame());
	};
}; //fin función

public function getActiveGame():Game {
	return Play(state).getActiveGame();
}; //fin función

public function getPlayerCredits():Number {
	//trace("SGS.as | getPlayerCredits ");
	return Play(state).getPlayer().getBalance().getCredit();
}; //fin función

public function setPlayerCredits(betCredits:Number, credits:Number):Void {
	trace("SGS.as | setPlayerCredits "+betCredits+" "+credits);
	bridge.SGS_2_SWF_setPlayerCredits(betCredits, credits);
}; //fin función


public function roundEnd():Void {
	Communication.getActive().getPlayerFounds();
}; //fin función

public function gameLogIn(gameSlot:GameSlotInterface):Void {
	this.gameSlot=gameSlot;
	Communication.getActive().gameLogIn(Active(state).getActiveGame().getSerial());
}; //fin función

public function gameLogOut():Void {
	Communication.getActive().gameLogOut(Active(state).getActiveGame().getSerial());
	bridge.SGS_2_SWF_gameLogOut();
	bridge.SGS_2_SWF_loadGames(Enabled(state).getGames());
}; //fin función


public function event(event:CommunicationEvent) {
		
		if (event.isEnabled()) {
			//trace("SGS.as | eventEnabled");
			var _state:State=new Enabled();
			if(event.isActive()){ _state=new Active(Enabled(this.state)); }//trace("SGS.as | eventActive");}
			if(event.isPlay()){ _state=new Play(Active(this.state)); }//trace("SGS.as | eventPlay");}
			
			try	{
				_state.init();	
				this.setState(_state);
				bridge.SGS_2_SWF_enableMachine();
			} catch (e) {
				trace("SGS.as | event() | error= "+e);
			}
						
		} else if (event.isDisabled()) {
			
			var _state:State=new Disabled();
			//Inicializo el nuevo estado;
			if (!_state.init(event.getException())) return;			
			//Seteo el nuevo estado.
			setState(_state);
			bridge.SGS_2_SWF_disableMachine("Tiempo de espera excedido");
		
		} else if (event instanceof CommunicationEventGames) {
				//trace("SGS.as | event instanceof CommunicationEventGames");
				try{
					Enabled(state).setGames(CommunicationEventGames(event).getGames());
					bridge.SGS_2_SWF_loadGames(Enabled(state).getGames());
				} catch(e) {
					//trace("Es de otro estado "+e);
				}
		
		} else if(event instanceof CommunicationEventPlayer) {
				//trace("SGS.as | CommunicationEventPlayer");
				Active(state).setPlayer(CommunicationEventPlayer(event).getPlayer());
				bridge.SGS_2_SWF_loadPlayer(Active(state).getPlayer());
				gameSlot.setTokens(Active(state).getPlayer().getBalance().getCredit());
				//trace("SGS.as | Play(getState())= "+Play(state));
				
		} else if(event instanceof CommunicationEventGameResult) {
			Play(state).setResult(CommunicationEventGameResult(event).getResult());
			
			if(CommunicationEventGameResult(event).getNoRound()){
				gameSlot.setRules(Play(state).getRules());
				gameSlot.init(Play(state).getResult());
			} else {
				gameSlot.result(Play(state).getResult());
			}
		};
		
} // fin función

public function eventException(event:GSAException) {
		if(event instanceof CommunicationTimeOutException){
			bridge.SGS_2_SWF_disableMachine("Tiempo de espera excedido");
			bridge.SGS_2_SWF_lostCommunication();
			Communication.getActive().doCommunication();
			
		}
		if(event instanceof CommunicationFailException){
			bridge.SGS_2_SWF_disableMachine("No se pudo efectuar la conexión");
			bridge.SGS_2_SWF_lostCommunication();
			Communication.getActive().doCommunication();
		}
		if(event instanceof CommunicationLostException){
			bridge.SGS_2_SWF_disableMachine("Conexión Perdida");
			bridge.SGS_2_SWF_lostCommunication();
			Communication.getActive().doCommunication();
		};
} // fin función

} // fin de clase